<?php
use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
$institution_code = Mall::getCurrentInstitutionCode();
$this->title = $site_name.'->商品详情';
?>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall/goods-detail.css"/>
<script src="<?= $web ?>/js/mall/goods-detail.js" type="text/javascript" charset="utf-8"></script>

<!---------------------------------------------------->
<div class="goods">
    <div class="right-extra">
        <div>
            <div id="preview" class="spec-preview"> <span class="jqzoom"><img jqimg="<?= $goods['goods_image'] ?>" src="<?= $goods['goods_image'] ?>" /></span> </div>
            <p>鼠标移至图片可放大图片</p>
            <div class="spec-scroll">
                <a class="prev">&lt;</a>
                <a class="next">&gt;</a>
                <div class="items">
                    <ul>
                        <?php
                        foreach ($goods['goods_image_list'] as $item) {
                            ?>
                            <li><img alt="图片" bimg="<?= $item['image_url'] ?>" src="<?= $item['image_url'] ?>" onmousemove="preview(this);"></li>
                            <?php
                        }
                        ?>
                        <!--<li><img alt="图片" bimg="imgs/b1.jpg" src="imgs/s1.jpg" onmousemove="preview(this);"></li>-->
                    </ul>
                </div>
            </div>
        </div>

    </div>

    <div class="goods-referral fl">
        <p class="title"><?php
            if ($goods['is_crossborder'] == '1') {
                ?>
                <span class="cross-title-icon">跨境</span>
                <?php
            }
            ?><?= $goods['name'] ?>
        </p>
        <?php
        if ($goods['is_activity'] == '1') {
            ?>
            <p class="old-integral"><?= intval($goods['old_price']) ?> <?= $integral_unit ?></p>
            <?php
        }
        ?>

        <p><span class="big-red"><?= intval($goods['selling_integral']) ?></span> <?= $integral_unit ?></p>
        <div class="goods-message">
            <p class="fl delivery">配送:<span>全场免运费</span></p>
            <p class="fl stock">库存:<span>正在获取..</span></p>
            <p class="fl code"> 产品代码：<span><?= $goods['code'] ?></span></p>
        </div>
        <div class="goods-number">
            <p class="fl">数量：</p>
            <div class="gw-num fl">
                <span class="jian">-</span>
                <input type="text" value="1" class="num" id="txt_quantity" readonly="readonly"/>
                <span class="add">+</span>
            </div>
        </div>
        <script type="text/javascript">
            $(function() {
                $(".add").click(function() {
                    var n = $(this).prev().val();
                    if(n>=stock){
                        return;
                    }
                    var num = parseInt(n) + 1;
                    if(num == 0) {
                        return;
                    }
                    if(num > 1) {
                        $(".jian").css("color", "#8a8a8a");
                    }

                    $(this).prev().val(num);
                });
                $(".jian").click(function() {
                    var n = $(this).next().val();
                    var num = parseInt(n) - 1;
                    if(num == 0) {
                        return
                    }
                    if(num == 1) {
                        $(".jian").css("color", "#E2E2E2");
                    }
                    $(this).next().val(num);
                });
            });
        </script>
        <div class="clfl"></div>
        <?php
        if ($goods['is_crossborder'] == '1') {
            ?>
            <!--------------1.3---------------->
            <p class="prompt">提示：<span>跨境购买须知</span><img src="<?= $web ?>/images/mall/cross-border.png"/></p>
            <div class="cross-border">
                <p class="cross-border-name">跨境保税商品购买须知</p>
                <p>一、什么是跨境保税商品</p>
                <p>
                    跨境保税商品是采购自国外的商品，进境时海关、国检都会对商品的品质做审查（品质保证），进境后存放于保税区仓库，国内消费者通过电子商务平台下单购买后，在海关全程监管下打包出区，最终交由国内快递送至消费者手上。优点：商品进境时海关、国检对商品审查，商品存放保税仓库（绝无假货，品质保证）</p>
                <p>二、购买跨境保税商品要求</p>
                <p>1、单笔订单不超过<span>2000元人民币</span>；</p>
                <p>2、每人次一年不能购买超过2万元人民币跨境保税商品（按身份证号计算）；</p>
                <p>3、根据中国海关总署要求，您所购买的商品需提供<span>身份证信息</span>进行入境申报，请您配合。我们不会向第三方泄露您的资料，请您放心。</p>
                <p>三、跨境商品物流配送</p>
                <p>跨境产品由于海关检验的特殊性，物流送达时间一般时效为：</p>
                <p>保税区邮：<br/>保税区—圆通，时效<span>5-7天</span></p>
                <p>香港直邮：<br/>香港外包仓—申通快递，时效<span>10天左右</span><br/>香港汇通仓—汇通快递，时效<span>8-10天</span><br/>香港—圆通，时效<span>10-12天</span>
                </p>
                <p>
                    海外直邮：<br/>日本直邮—EMS，时效<span>5-7天</span><br/>日本—国际段联邦国内转邮政小包或韵达，时效<span>10-13天</span><br/>荷兰—中欧快运，时效<span>10-15天</span>
                </p>
                <p class="reminder-name">温馨提示：</p>
                <p>由于海关系统升级于<span>16年12月30日18:00</span>后兑换的跨境产品，需要<span>17年1月10日</span>开始发货，给您带来的不变敬请谅解。</p>
                <img src="<?= $web ?>/images/mall/cross-border-arrows.jpg"/>
            </div>
        <?php
        }
        ?>

        <script type="text/javascript">
            $(function() {
                $(".prompt").mouseover(function() {
                    $(".cross-border").show();
                });
                $(".prompt").mouseout(function() {
                    $(".cross-border").hide();
                });
            });
        </script>
        <div class="btn">
            <input type="button" id="btn_add_cart" class="first-btn" value="加入购物袋" />
            <!--<input type="button" class="second-btn" value="立即兑换" />-->
        </div>
        <div class="safeguard">
            <img src="<?= $web ?>/images/mall/safeguard-img.jpg" />
        </div>

    </div>
</div>

<div class="history-and-details">
    <div class="goods-history">
        <p class="history-name">您浏览过的历史商品</p>
        <div class="goods-history-list">
            <ul>
                <!--<li>
                    <a href=""><img src="img/goods-history-img.jpg" /></a>
                    <div class="goods-history-text">
                        <p class="goods-history-title">万希 韩版简约时尚百鞋</p>
                        <p class="goods-history-integral">188 积分</p>
                    </div>
                </li>-->
            </ul>
        </div>
    </div>
    <div class="goods-details">
        <p class="title-name">商品详情</p>
        <!----详情内容----->
        <div class="details-content">
            <?= $goods['detail_html'] ?>
        </div>

    </div>
</div>
<script>
    //历史足迹
    $(function ($) {

//        myStorage.clear();
        //展示历史足迹
        if (myStorage) {
            //判断是否同一机构
            var institutionCode = '<?=$institution_code?>';
            if (myStorage.institution_code) {
                if (myStorage.institution_code != institutionCode) {
                    myStorage.clear();
                }

            } else {
                myStorage.institution_code = institutionCode;
            }

            if (myStorage.his_goods_array) {
                var his_goods_array = JSON.parse(myStorage.his_goods_array);
                var data = {};
                data.goods_code_list = his_goods_array;
                requestJsonMallM('get-his-goods-handler', data, true, function (response) {
                    if (response.status == 0) {
                        var goods_list = response.data;
                        var html = '';
                        for (var j = 0; j < his_goods_array.length; j++) {
                            for (var i = 0; i < goods_list.length; i++) {
                                if (his_goods_array[his_goods_array.length - 1 - j] == goods_list[i].code) {
                                    html += '<li><a  href="goods-detail?goods_code=' + goods_list[i].code + '&institution_code=<?=$institution_code?>">';
                                    html += '<img src="' + goods_list[i].image_url + '@300w"/></a>';
                                    var crossborder_span = '';
                                    if (goods_list[i].is_crossborder == 1) {
                                        crossborder_span = '<span class="cross-title-icon">跨境</span>';
                                    }
                                    html += '<div class="goods-history-text"><p class="goods-history-title">' + crossborder_span + goods_list[i].name + '</p>';
                                    html += '<p class="goods-history-integral">' + goods_list[i].integral_unit + ':' + parseInt(goods_list[i].selling_integral) + '</p></div>';
                                    html += '</li>';
                                    break;
                                }
                            }
                        }
                        $(".goods-history-list ul").append(html);
                    }
                });
            }
        }

        //添加当前商品访问记录,显示10个
        if (myStorage) {
            var goods_code = "<?=$goods['code']?>";
            if (goods_code) {
                var his_goods_array = [];
                var new_goods_array = [];
                if (myStorage.his_goods_array != undefined) {
                    his_goods_array = JSON.parse(myStorage.his_goods_array);
                }

                if (his_goods_array.length > 0) {
                    //只显示9个
                    var temp_array = [];
                    var j = 0;
                    for (var i = 0; i < his_goods_array.length; i++) {
                        if (his_goods_array[his_goods_array.length - 1 - i] != goods_code) {
                            temp_array.push(his_goods_array[his_goods_array.length - 1 - i]);
                            j = j + 1;
                            if (j == 8) {
                                break;
                            }
                        }
                    }

                    for (var i = 0; i < temp_array.length; i++) {
                        new_goods_array.push(temp_array[temp_array.length - 1 - i]);
                    }
                }

                new_goods_array.push(goods_code);
                myStorage.his_goods_array = JSON.stringify(new_goods_array);
            }
        }


    });
    //删除历史足迹
    function delHisNoStockGoods(goods_code) {
        var his_goods_array = [];
        if (myStorage.his_goods_array != undefined) {
            his_goods_array = JSON.parse(myStorage.his_goods_array);
            his_goods_array.remove(goods_code);
        }
    }
</script>
<script>
    setBtnCart=function(v){
        if(v){
            $('.stock').removeClass('stock_no');
            $('#btn_add_cart').removeClass('second-btn');
            $('#btn_add_cart').addClass('first-btn');
            $('#btn_add_cart').click(function(){
                addCart('<?= $goods['code'] ?>',$('#txt_quantity').val());
            });
            // $('#setOptions').closest('a').attr('href', "javascript:addCart('<?= $goods['code'] ?>')");
        }else{
            $('.stock').addClass('stock_no');
            $('#btn_add_cart').removeClass('first-btn');
            $('#btn_add_cart').addClass('second-btn');
        }
    };


    $(function($){
        getGoodsStock();
    });
    var stock=0;
    function getGoodsStock() {
        setBtnCart(false);
        var goods_code = '<?= $goods['code'] ?>';
        requestJson('get-goods-stock-handle', {'goodsCode': goods_code}, true, function (re) {
            stock =  re.data;
            var stockTxt = '';
//            stock=0;
            if (<?=$goods['is_shelved'] == '0' || $goods['is_show'] == '0' || $goods['is_enabled'] == '0' ? 'true' : 'false'?>) {
                $('.stock span').html('商品不存在');
                delHisNoStockGoods(goods_code);
                return;
            }
            if (stock <= 0) {
                stockTxt = '缺货';
                delHisNoStockGoods(goods_code);
            } else {


                if (stock >= 20) {
                    stockTxt = '库存充足';
                } else {
                    stockTxt = '库存紧张';
                }
                setBtnCart(true);

            }

            $('.stock span').html(stockTxt);

        });
    }
    function addCart(goods_code,quantity) {
        var data = {};
        data.goodsCode = goods_code;
        data.quantity = quantity || 1;
        var re = requestJsonMallM('add-cart-handle', data);
        if (re.status == 0) {
            refreshCartGoodsCount();
//             alert('添加成功');
            showAddCartSuccess();
        } else {
            alert(re.message);
        }
    }
</script>
<div class="join-success-window" style="display: none;">
    <div class="join-content">
        <img class="join-icon" src="<?=$web?>/images/mall/join-success-icon.png"/>
        <p class="txt">商品已成功加入购物袋！</p>
    </div>
    <div class="join-shade"></div>
</div>

<script type="text/javascript">
    showAddCartSuccess=function(){
        $(".join-success-window").fadeIn();
        setTimeout(function(){
            $(".join-success-window").fadeOut();
        }, 1000);
    };
</script>
